/*
 * JivaBenchUI.java
 *
 * Created on April 2, 2007, 4:38 PM
 */

package net.xofar.jiva.ui;

import javax.swing.UIManager;

import net.xofar.jiva.GAProblem;
import net.xofar.jiva.population.Chromosome;
import net.xofar.jiva.population.Population;
import net.xofar.util.listener.EventListener;

import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;

/**
 * 
 * @author lalitp
 */
public class JivaBenchUI
        extends javax.swing.JFrame
{

    /** Creates new form JivaBenchUI */
    public JivaBenchUI()
    {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code
    // <editor-fold defaultstate="collapsed" desc=" Generated Code
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        problemsCombo = new javax.swing.JComboBox(GAProblem.getProblems());
        runButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        popSize = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        chrSize = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        numIters = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        optimalSol = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        desc = new javax.swing.JTextArea();
        gaCanvasHolder = new javax.swing.JScrollPane();
        gaCanvas = new GACanvas();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Jiva Demo");
        setBounds(new java.awt.Rectangle(75, 75, 850, 600));
        setMinimumSize(new java.awt.Dimension(850, 600));
        jSplitPane1.setDividerLocation(325);
        jSplitPane1.setOneTouchExpandable(true);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("GA Problem Definition"));
        problemsComboActionPerformed(null);
        jLabel1.setText("Problem:");

        problemsCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                problemsComboActionPerformed(evt);
            }
        });

        runButton.setText("Run");
        runButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                runButtonActionPerformed(evt);
            }
        });

        jLabel2.setText("Population Size:");

        popSize.setEditable(false);

        jLabel3.setText("Chromosome Size");

        chrSize.setEditable(false);

        jLabel4.setText("Num Evolutions");

        numIters.setEditable(false);

        jLabel5.setText("Optimal Solution:");

        optimalSol.setEditable(false);

        jLabel6.setText("Description:");

        desc.setColumns(20);
        desc.setEditable(false);
        desc.setFont(new java.awt.Font("SansSerif", 0, 12));
        desc.setLineWrap(true);
        desc.setRows(5);
        desc.setWrapStyleWord(true);
        jScrollPane1.setViewportView(desc);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(22, 22, 22)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel2)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel1)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel3)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jLabel4)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(jLabel6)
                                .add(jLabel5)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(popSize, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                            .add(problemsCombo, 0, 179, Short.MAX_VALUE)
                            .add(chrSize, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                            .add(numIters, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
                            .add(optimalSol, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap(245, Short.MAX_VALUE)
                        .add(runButton)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(problemsCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(popSize, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(chrSize, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(numIters, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(optimalSol, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel6)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 169, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(runButton)
                .addContainerGap(62, Short.MAX_VALUE))
        );
        jSplitPane1.setLeftComponent(jPanel1);

        gaCanvasHolder.setBorder(javax.swing.BorderFactory.createTitledBorder("GA Solution Trace"));
        org.jdesktop.layout.GroupLayout gaCanvasLayout = new org.jdesktop.layout.GroupLayout(gaCanvas);
        gaCanvas.setLayout(gaCanvasLayout);
        gaCanvasLayout.setHorizontalGroup(
            gaCanvasLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 448, Short.MAX_VALUE)
        );
        gaCanvasLayout.setVerticalGroup(
            gaCanvasLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 471, Short.MAX_VALUE)
        );
        gaCanvasHolder.setViewportView(gaCanvas);

        jSplitPane1.setRightComponent(gaCanvasHolder);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 756, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
                .addContainerGap())
        );
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-850)/2, (screenSize.height-600)/2, 850, 600);
    }// </editor-fold>//GEN-END:initComponents

    private void problemsComboActionPerformed(java.awt.event.ActionEvent evt)
    {// GEN-FIRST:event_problemsComboActionPerformed
        GAProblem<?> problem = (GAProblem<?>)problemsCombo.getSelectedItem();
        popSize.setText(String.valueOf(problem.getPopulationSize()));
        chrSize.setText(String.valueOf(problem.getChromosomeSize()));
        numIters.setText(String.valueOf(problem.getNumEvolutions()));
        optimalSol.setText(String.valueOf(problem.getOptimalSolution()));
        desc.setText(problem.getDescription());
        desc.setCaretPosition(0);
        ((GACanvas)gaCanvas).clear();
    }// GEN-LAST:event_problemsComboActionPerformed

    private void runButtonActionPerformed(java.awt.event.ActionEvent evt)
    {// GEN-FIRST:event_runButtonActionPerformed
        runButton.setEnabled(false);
        new Thread(new Runnable()
        {
            public void run()
            {
                GAProblem<?> problem = (GAProblem<?>)problemsCombo
                        .getSelectedItem();
                run(problem);
            }

            private <T> void run(GAProblem<T> problem)
            {
                GACanvas gac = (GACanvas)gaCanvas;
                gac.init(problem.getNumEvolutions(), problem.getOptimalSolution());
                EventListener<Population<T>> listener = new UiGAListener<T>(gac);
                Population<T> finalPop = problem.getEvolver().run(problem,
                        listener);

                Chromosome<T> fittest = finalPop.determineFittestChromosome();
                double best = fittest.getFitnessValue();
                System.out.format(
                        "\nFittest Chromosome in Final generation is: %s\n",
                        fittest.toString());
                System.out.format("Fittest Chromosome has value: %.3f\n", best);
                runButton.setEnabled(true);
            }
        }).start();

    }// GEN-LAST:event_runButtonActionPerformed

    /**
     * @param args
     * the command line arguments
     */
    public static void main(String args[])
            throws Exception
    {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new JivaBenchUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField chrSize;
    private javax.swing.JTextArea desc;
    private javax.swing.JPanel gaCanvas;
    private javax.swing.JScrollPane gaCanvasHolder;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTextField numIters;
    private javax.swing.JTextField optimalSol;
    private javax.swing.JTextField popSize;
    private javax.swing.JComboBox problemsCombo;
    private javax.swing.JButton runButton;
    // End of variables declaration//GEN-END:variables

}
